#!/usr/bin/perl -w

#=======================================================================
# enc-mp4
# File ID: ccf5e412-f742-11dd-b99b-000475e441b9
# Encode videos for Nokia N95.
#
# Character set: UTF-8
# ©opyleft 2008– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later, see end of 
# file for legal stuff.
#=======================================================================

use strict;
use Getopt::Long;

$| = 1;

our $Debug = 0;

our %Std = (
    'encoder' => "ffmpeg",
    'frames-per-second' => 15,
);

our %Opt = (

    'comment' => "",
    'debug' => 0,
    'encoder' => $Std{'encoder'},
    'force' => 0,
    'frames-per-second' => $Std{'frames-per-second'},
    'help' => 0,
    'output-file' => "",
    'verbose' => 0,
    'version' => 0,

);

our $progname = $0;
$progname =~ s/^.*\/(.*?)$/$1/;
our $VERSION = "0.00";

Getopt::Long::Configure("bundling");
GetOptions(

    "comment|c=s" => \$Opt{'comment'},
    "debug" => \$Opt{'debug'},
    "encoder|e=s" => \$Opt{'encoder'},
    "force|f" => \$Opt{'force'},
    "frames-per-second|fps=i" => \$Opt{'frames-per-second'},
    "help|h" => \$Opt{'help'},
    "output-file|o=s" => \$Opt{'output-file'},
    "verbose|v+" => \$Opt{'verbose'},
    "version" => \$Opt{'version'},

) || die("$progname: Option error. Use -h for help.\n");

$Opt{'debug'} && ($Debug = 1);
$Opt{'help'} && usage(0);
if ($Opt{'version'}) {
    print_version();
    exit(0);
}

if ($#ARGV >= 0) {
    for (@ARGV) {
        process_file($_);
    }
} else {
    process_file("-");
}

sub process_file {
    # {{{
    my $Filename = shift;
    D("process_file('$Filename')");
    my $use_stdin = ($Filename eq "-") ? 1 : 0;
    my $Comment = length($Opt{'comment'})
        ? $Opt{'comment'}
        : "";
    my $Fps = length($Opt{'frames-per-second'})
        ? $Opt{'frames-per-second'}
        : $Std{'frames-per-second'};
    my $output_file = length($Opt{'output-file'})
        ? $Opt{'output-file'}
        : $use_stdin
            ? "-"
            : "$Filename.mp4";
    if ($use_stdin || -e $Filename) {
        if (-e "$Filename.mp4" && !$Opt{'force'}) {
            warn("$progname: $Filename.mp4: Will not overwrite existing file. Use --force (-f) option to override.\n");
            return;
        }
        msg(1, "Encoding \"$Filename\" to \"$output_file\"...");
        if ($Opt{'encoder'} eq "ffmpeg") {
            # Modified version of something found at 
            # <http://www.n95users.com/forum/general-95/2602-howto-convert-avi-nokia-n95-compatible-avi-embedded-subtitle-linux.html>. 
            # Does not keep aspect ratio, fills the entire N95 screen, 
            # but that’s probably better than using 30% of the screen in 
            # extreme cases.
            my @Cmd = (
                "ffmpeg",
                "-i", $Filename,    # Input filename
                "-f", "mp4",        # Format
                "-vcodec", "mpeg4", # Video codec
                "-b", "250000",     # Video bitrate
                "-r", $Fps,         # Frames per second
                "-s", "320x240",    # Frame size
                "-acodec", "aac",   # Audio codec
                "-ar", "44100",     # Audio sampling frequency
                "-ab", "64",        # Audio bitrate in kbit/s
                "-ac", "2",         # Number of audio channels
                "-me", "epzs",      # Motion estimation method (best quality)
                "-y",               # Overwrite output files
                $output_file        # Output filename
            );
            if (length($Comment)) {
                splice(@Cmd, 1, 0, "-comment", $Comment);
            }
            mysyst(@Cmd);
        } elsif ($Opt{'encoder'} eq "mencoder") {
            # Recipe created by Tomasz Sterna, found at 
            # <http://notes.xiaoka.com/2008/03/29/encode-mp4-files-for-nokia-n95-with-mencoder/>.
            # Has sound sync issues.
            my @Cmd = (
                "mencoder",
                "-of", "lavf",
                "-lavfopts", "format=mp4",
                "-oac", "lavc",
                "-ovc", "lavc",
                "-lavcopts", "aglobal=1:vglobal=1:acodec=libfaac:abitrate=128:vcodec=mpeg4:keyint=25",
                "-ofps", $Fps,
                "-af", "lavcresample=44100",
                "-vf", "harddup,scale=320:-3",
                "-mc", "0",
                "-noskip",
                $Filename,
                "-o",
                $output_file
            );
            if (length($Comment)) {
                splice(@Cmd, 1, 0, "-info", "comment=$Comment");
            }
            mysyst(@Cmd);
        } else {
            die("$progname: $Opt{'encoder'}: Unknown --encoder value.\n");
        }
    } else {
        warn("$progname: $Filename: File not found");
    }
    return;
    # }}}
} # process_file()

sub mysyst {
    # Customised system() {{{
    my @Args = @_;
    my $system_txt = sprintf("system(\"%s\");", join("\", \"", @Args));
    msg(2, "Executing \"" . join(" ", @_) . "\"...");
    system(@_);
    # }}}
} # mysyst()

sub print_version {
    # Print program version {{{
    print("$progname v$VERSION\n");
    # }}}
} # print_version()

sub usage {
    # Send the help message to stdout {{{
    my $Retval = shift;

    if ($Opt{'verbose'}) {
        print("\n");
        print_version();
    }
    print(<<END);

Usage: $progname [options] [file [files [...]]]

Encode movies for the Nokia N95. If no files are specified, the program 
encodes from stdin to stdout.

Options:

  -c x, --comment x
    Use x as comment in the encoded file.
  -e x, --encoder x
    Use encoder x:
      ffmpeg
      mencoder (may have audio/video sync problems)
    Default: $Std{'encoder'}
  --fps x, --frames-per-second x
    Generate video with x frames per second.
    Default: $Std{'frames-per-second'}.
  -h, --help
    Show this help.
  -f, --force
    Overwrite existing files (input filename with .mp4 extension).
  -o x, --output-filename x
    Use x as output filename.
  -v, --verbose
    Increase level of verbosity. Can be repeated.
  --version
    Print version information.
  --debug
    Print debugging messages.

END
    exit($Retval);
    # }}}
} # usage()

sub msg {
    # Print a status message to stderr based on verbosity level {{{
    my ($verbose_level, $Txt) = @_;

    if ($Opt{'verbose'} >= $verbose_level) {
        print(STDERR "$progname: $Txt\n");
    }
    # }}}
} # msg()

sub D {
    # Print a debugging message {{{
    $Debug || return;
    my @call_info = caller;
    chomp(my $Txt = shift);
    my $File = $call_info[1];
    $File =~ s#\\#/#g;
    $File =~ s#^.*/(.*?)$#$1#;
    print(STDERR "$File:$call_info[2] $$ $Txt\n");
    return("");
    # }}}
} # D()

__END__

# Plain Old Documentation (POD) {{{

=pod

=head1 NAME



=head1 SYNOPSIS

 [options] [file [files [...]]]

=head1 DESCRIPTION



=head1 OPTIONS

=over 4

=item B<-h>, B<--help>

Print a brief help summary.

=item B<-v>, B<--verbose>

Increase level of verbosity. Can be repeated.

=item B<--version>

Print version information.

=item B<--debug>

Print debugging messages.

=back

=head1 BUGS



=head1 AUTHOR

Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.

=head1 COPYRIGHT

Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
This is free software; see the file F<COPYING> for legalese stuff.

=head1 LICENCE

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation, either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.
If not, see L<http://www.gnu.org/licenses/>.

=head1 SEE ALSO

=cut

# }}}

# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
